package com.utilities;

import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.script.ScriptException;
import javax.swing.ImageIcon;
import javax.swing.JFormattedTextField;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import org.apache.log4j.Logger;


/**
 * 
 * @Title: ToolUtil.java
 * @Description: org.app.ticket.util
 * @Package org.app.ticket.util
 * @date 2012-10-26
 * @version V1.0
 * 
 */
public class ToolHelper {
	private static final Logger logger = Logger.getLogger(ToolHelper.class);

	/**
	 * èŽ·å�–ImageIcon
	 * 
	 * @param path
	 * @return
	 */
	public static ImageIcon getImageIcon(String path) {
		ImageIcon icon = new ImageIcon(path);
		icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(), icon.getIconHeight(), Image.SCALE_DEFAULT));
		return icon;
	}

	/**
	 * éªŒè¯�æŽ§ä»¶æ˜¯å�¦ä¸ºç©º
	 * 
	 * @return
	 */
	public static List<String> validateWidget(Object... o) {
		List<String> msg = new ArrayList<String>();
		if (o.length > 0) {
			for (Object s : o) {
				JTextComponent f = (JTextComponent) s;
				if (StringHelper.isEmptyString(f.getText().trim())) {
					msg.add(f.getToolTipText());
				}
				if (f.getClass() == JFormattedTextField.class) {
				//	if (StringHelper.isEqualString(Constants.SYS_STRING_DATEFORMAT, f.getText().trim())) {
				//		msg.add(f.getToolTipText());
				//	}
				}
			}
		}
		return msg;
	}

	/**
	 * èŽ·å�–ç”¨æˆ·ä¿¡æ�¯
	 * 
	 * @param fileName
	 * @param o
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	public static void getUserInfo(String path, String fileName, Object... o) throws Exception {
		Map<String, String> userMap = new HashMap<String, String>();
		if (o.length > 0) {
			for (Object s : o) {
				JTextComponent f = (JTextComponent) s;
				if (!StringHelper.isEmptyString(f.getText().trim())) {
					if (f.getClass() == JTextField.class) {
						JTextField jtf = (JTextField) f;
						logger.debug("[key = " + jtf.getName() + ",value = " + jtf.getText().trim() + "]");
						userMap.put(jtf.getName(), jtf.getText().trim());
					}
					if (f.getClass() == JPasswordField.class) {
						JPasswordField jpf = (JPasswordField) f;
						logger.debug("[key = " + jpf.getName() + ",value = " + jpf.getText().trim() + "]");
						userMap.put(jpf.getName(), jpf.getText().trim());
					}
				}
			}
		}

		writeFile(userMap, path, fileName);
	}

	/**
	 * å°†ç”¨æˆ·ä¸Šä¸€æ¬¡ä½¿ç”¨ä¿¡æ�¯å†™å…¥æ–‡ä»¶
	 * 
	 * @param info
	 * @param fileName
	 * @throws Exception
	 */
	private static void writeFile(Map<String, String> userMap, String path, String fileName) throws Exception {
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(path + fileName));
		out.writeObject(userMap);
		out.close();
	}

	/**
	 * å¯¹æŽ§ä»¶è¿›è¡Œèµ‹å€¼
	 * 
	 * @param path
	 * @param fileName
	 * @param o
	 * @throws Exception
	 * @throws FileNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public static void setUserInfo(String path, String fileName, Object... o) throws FileNotFoundException, Exception {
		if (!new File(path + fileName).exists()) {
			return;
		}
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(path + fileName));
		Map<String, String> userMap = (Map<String, String>) in.readObject();
		in.close();
		if (o.length > 0) {
			for (Object s : o) {
				JTextComponent f = (JTextComponent) s;
				if (f.getClass() == JTextField.class) {
					JTextField jtf = (JTextField) f;
					jtf.setText(userMap.get(jtf.getName()) == null ? "" : userMap.get(jtf.getName()));
				}
				if (f.getClass() == JPasswordField.class) {
					JPasswordField jpf = (JPasswordField) f;
					jpf.setText(userMap.get(jpf.getName()) == null ? "" : userMap.get(jpf.getName()));
				}
			}
		}
	}

	/**
	 * 
	 * èŽ·å�–åŸŽå¸‚çš„Unicode
	 * 
	 * @param cityName
	 * @param cityCode
	 * @return
	 */
	public static String getUnicode(String cityName, String cityCode) {
		String ret = "";
		String result = "";
		for (int i = 0; i < cityName.length(); i++) {
			int chr1 = (char) cityName.charAt(i);
			if (chr1 >= 19968 && chr1 <= 171941) {// æ±‰å­—èŒƒå›´ \u4e00-\u9fa5 (ä¸­æ–‡)
				result += "%u" + Integer.toHexString(chr1).toUpperCase();
			} else {
				result += cityName.charAt(i);
			}
		}
		ret = result + "%2C"+cityCode;
		return ret;
	}
	
	/**
	 * 
	 * èŽ·å�–DynamicJSä¸­çš„VALUE
	 * 
	 * @param key
	 * @return
	 * @throws IOException
	 * @throws ScriptException
	 */
	public static String getSeatJsValue(String mark, String seat) throws IOException, ScriptException {
	//	String js = ResManager.jsStrings[0];
	//	String value = ScriptRunHelper.runSeatJsMethod(mark, seat, js);
		return "";
	}
	
	public static void main(String[] args) throws IOException, ScriptException {
		getSeatJsValue("1018553234405575000010185503733035650000","1");
	}
}
